package com.ruoyi.web.controller.Base;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.Ability_SubscribeInfor;
import com.ruoyi.service.Ability_SubscribeInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subscribe")
public class SubsribeController extends BaseController {
    @Autowired
    Ability_SubscribeInforService ability_SubscribeInforService;
    @PostMapping("/add")
    public AjaxResult addSubscribe(@RequestBody Ability_SubscribeInfor ability_SubscribeInfor)
    {
        System.out.println(ability_SubscribeInfor.getServiceName());
        int k=ability_SubscribeInforService.insertAbility_SubscribeInfor(ability_SubscribeInfor);

        if(k==1)
        {
            return AjaxResult.success("添加成功");
        }
        else if(k==-101)
        {
            return AjaxResult.error("服务不存在或拼写错误");
        }else if(k==-102){
            return AjaxResult.error("用户不存在或拼写错误");
        }else if(k==-104){
            return AjaxResult.error("已经该订阅信息");
        }
        return AjaxResult.error("添加失败");
    }
@GetMapping("/list")
    public TableDataInfo getList(@RequestParam(required = false ,value = "sername") String name,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, // 添加默认值可选
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
       startPage();
    Ability_SubscribeInfor ability_SubscribeInfor=new Ability_SubscribeInfor();
    ability_SubscribeInfor.setUserAccount(name);
    List<Ability_SubscribeInfor> list=   ability_SubscribeInforService.getAbility_SubscribeInforList(ability_SubscribeInfor);
        return getDataTable(list);
    }
@GetMapping("/del")
    public AjaxResult del(@RequestParam Integer id){

      int k=  ability_SubscribeInforService.deleteAbility_SubscribeInforById(id);
      if(k==0){
          return AjaxResult.error("删除失败，服务器发生错误");
      }else if(k==-1){
          return  AjaxResult.error("删除失败，代理服务器发生错误");
      }else{
        return AjaxResult.success("删除成功");}
    }
 @PostMapping("/update")
    public AjaxResult upd(@RequestBody Ability_SubscribeInfor abilitySubscribeInfor){
        int k=ability_SubscribeInforService.updateAbility_SubscribeInfor(abilitySubscribeInfor);
        if(k>0){
        return     AjaxResult.success("修改成功");
        }
        return  AjaxResult.error("修改失败");
    }
  @PostMapping("/app")
    public AjaxResult app(@RequestBody Ability_SubscribeInfor abilitySubscribeInfor){
        int k=ability_SubscribeInforService.Approved(abilitySubscribeInfor);
        if(k==-2){
         return    AjaxResult.error("订阅的服务不存在或已经停止服务");
        } else if(k==-3){
         return    AjaxResult.error("发生错误，用户不存在");
      } else if (k==-4) {
        return     AjaxResult.error("订阅信息不存在，或已经被删除，请刷新页面");
        } else if (k>0) {
          return   AjaxResult.success("审核通过！");
        } else if (k==-5) {
            return AjaxResult.error("已经通过审核，请刷新页面");
        }
      return AjaxResult.error("服务器发生错误，请稍后再试，审核失败!");
    }
}
